import SJGNodeV3=require('./SJGNodeV3');
import GCJ_CLASS_TYPE=require('./../GCJ_CLASS_TYPE');
import Serializer=require('../../kernel/base/Serializer')
import SJGNode=require('./../tg/SJGNode');
//
export = SJGTGV3;
/**
 * 题纲对象
 * @author 
 *
 */
class SJGTGV3 extends Serializer{
    public root: SJGNodeV3=null;
    public version: string="3";
    public name: string="";
    
    public constructor(g?) {
        super(g);
        
        this.ct=GCJ_CLASS_TYPE.CLASS_TYPE_SJGTGV3;
	}
	// 检查数据
	public checkType():Boolean{
        return this.root.checkType();
	}
    public checkPage(): Boolean {
        return this.root.checkPage();
    }
    // 从json对象(注意不是json字符串)中解析
    // 要求版本号>=3
    public fromJson(jsonObj,versionId) {
        if(versionId<SJGNode.VERSION_3) { versionId=SJGNode.VERSION_3 };
        
        super.fromJson(jsonObj,versionId);
        
        if(jsonObj.hasOwnProperty("root")) {
            this.root=new SJGNodeV3(this.getGlobalData());
            this.root.fromJson(jsonObj.root, versionId);
        }
        this.version=jsonObj.version;
        this.name=jsonObj.name;
    }
    // 转成json字符串
    public toJson():string{
        // 去掉父json字符串的头尾
        var superJson=super.toJson();
        superJson=superJson.substr(1,superJson.length-2);

        var ret='{';
        ret+=superJson;
         
        // version
        ret+=',';
        ret+='"version":';
        ret+=this.version;
        
        // name
        ret+=',';
        ret+='"name":'+'"';
        ret+=this.name+'"';

        // root
        if(this.root) {
            ret+=',';
            ret+='"root":';
            ret+=this.root.toJson();
        }

        ret+='}';
        return ret;
    }
    
    // 遍历第一个node，深度遍历优先
    private current: SJGNodeV3=null;
    public first():SJGNodeV3{
        this.root.clearVisit();
        this.current=this.root;
        
        return this.next();
    }
    // 遍历下一个node，深度遍历优先
    public next(): SJGNodeV3 {
        if(this.current==null) return null;
        
        /* 
         如果current被访问过，则退回父进行处理
         如果current未被访问过，
            如果current没有子，则current设置为访问，返回current
            如果current有子，则找出未访问的子；
                如果存在，则处理该子
                如果不存在，则current设置为访问，返回current
         */ 
        if(this.current.isVisit()){
            this.current=<SJGNodeV3>this.current.parent;
            return this.next();
        }else{
            var len=this.current.children.length; 
            if(len==0) {
                this.current.setVisit();
                return this.current;
            }else{
                var uvChild: SJGNodeV3=null;
                for(var i = 0; i < len; i++) {
                    var child: SJGNodeV3=<SJGNodeV3>this.current.children[i];
                    if(!child.isVisit()) { 
                        uvChild=child;
                        break;
                    }
                }
                if(uvChild) {
                    this.current=uvChild;
                    return this.next();
                }else{
                    this.current.setVisit();
                    return this.current;
                }
            }
        }
        
    }
    
    // 计算题纲树的特征值
    public calcEigenValue(): string {
        return JSON.stringify(this.calcEigenValueCore());
    }
    // 计算题纲树的特征值
    public calcEigenValueCore() {
        var eigen=[];
        var leafs: SJGNodeV3[]=<SJGNodeV3[]>this.root.getLeafNodes();
        var process={};// 处理过的节点
        for(var i=0;i<leafs.length;i++) {
            var leaf: SJGNodeV3=leafs[i];
            if(process.hasOwnProperty('"'+leaf.id+'"')) continue;

            if(!leaf.is_child_node_of_group_kind()) {
                // 普通页节点，直接搜集
                if(leaf.h){
                    // 如果是隐藏节点，则直接纪录 -1
                    eigen.push(-1);
                    process[leaf.id]=1; // 记录处理过的节点
                }else{
                    // type:自身（不包含子）的表现形式1(0无/1文字/2图片/3图表/4表格)
                    if(leaf.type==SJGNodeV3.SJGNODE_TYPE_1) {
                        eigen.push(leaf.value.max);
                        process[leaf.id]=1; // 记录处理过的节点
                    } else if(leaf.type==SJGNodeV3.SJGNODE_TYPE_2) {
                        eigen.push(1);
                        process[leaf.id]=1; // 记录处理过的节点
                    }                                        
                }
            } else {
                // 找到同一个数组下的同一级的所有叶节点，计算最大值
                var level=leaf.index();
                var grandSons=leaf.parent.parent.getGroupGrandSonOfLevel(level);
                var max=0;
                for(var k=0;k<grandSons.length;k++) {
                    var gs=grandSons[k];
                    if(gs.type==SJGNodeV3.SJGNODE_TYPE_1) {
                        max=max>gs.value.max? max:gs.value.max;
                    } else if(gs.type==SJGNodeV3.SJGNODE_TYPE_2) {
                        max=1;
                    }
                }
                eigen.push(max);

                // 记录处理过的节点
                for(var k=0;k<grandSons.length;k++) {
                    var gs=grandSons[k];
                    process[gs.id]=1;
                }
            }
        }
        return eigen;
    }
    
    // 更新数组节点的child_template
    public updateChildTemplate(){
        this.root.updateChildTemplate();
    }
	
    // 增加测试页
    public addTestData() {
        //var str='{"version":"3","root":{"kind":0,"obj_id":null,"value":null,"children":[{"kind":3,"obj_id":null,"value":{"min":0,"max":10,"value":"第五届国际互联网大会","type":20,"id":0,"ct":808},"children":[],"topic":"会议名称","page_index":0,"relations":[{"page_index":0,"obj_id":null},{"page_index":1,"obj_id":null}],"type":1,"id":"11"},{"kind":3,"obj_id":null,"value":{"pos":{"h":100,"an":0,"w":100,"y":0,"x":0,"ct":2},"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","ct":805},"children":[],"topic":"会议图片","page_index":1,"relations":[{"page_index":1,"obj_id":null}],"type":2,"id":"12"},{"kind":1,"obj_id":null,"value":{"min":0,"max":10,"value":"邀请嘉宾","type":20,"id":0,"ct":808},"children":[{"kind":2,"obj_id":null,"value":{"min":0,"max":10,"value":"嘉宾1","type":20,"id":0,"ct":808},"children":[{"kind":3,"obj_id":null,"value":{"min":0,"max":10,"value":"王先生","type":20,"id":0,"ct":808},"children":[],"topic":"名称1","page_index":2,"relations":[{"page_index":2,"obj_id":null}],"type":1,"id":"1311"},{"kind":3,"obj_id":null,"value":{"pos":{"h":100,"an":0,"w":100,"y":0,"x":0,"ct":2},"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","ct":805},"children":[],"topic":"头像1","page_index":2,"relations":[{"page_index":2,"obj_id":null}],"type":2,"id":"1312"}],"topic":"嘉宾1","page_index":2,"relations":[{"page_index":2,"obj_id":1}],"type":1,"id":"131"},{"kind":2,"obj_id":null,"value":{"min":0,"max":10,"value":"嘉宾2","type":20,"id":0,"ct":808},"children":[{"kind":3,"obj_id":null,"value":{"min":0,"max":10,"value":"王先生","type":20,"id":0,"ct":808},"children":[],"topic":"名称2","page_index":2,"relations":[{"page_index":2,"obj_id":1}],"type":1,"id":"1321"},{"kind":3,"obj_id":null,"value":{"pos":{"h":100,"an":0,"w":100,"y":0,"x":0,"ct":2},"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","ct":805},"children":[],"topic":"头像2","page_index":2,"relations":[{"page_index":2,"obj_id":1}],"type":2,"id":"1322"}],"topic":"嘉宾2","page_index":2,"relations":[{"page_index":2,"obj_id":1}],"type":1,"id":"132"}],"topic":"邀请嘉宾","page_index":2,"relations":[{"page_index":2,"obj_id":1}],"type":1,"id":"13"}],"topic":"会议邀请函","page_index":null,"relations":[],"type":1,"id":"1"},"name":"333"}';
        var str='{"ct":903,"version":3,"name":"test_v3","root":{"ct":904,"id":1,"topic":"会议邀请函","children":[{"ct":904,"id":11,"topic":"会议名称","children":[],"kind":3,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"会议名称","min":1,"max":4},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":0,"obj_id":4}]},{"ct":904,"id":12,"topic":"会议图片","children":[],"kind":3,"type":2,"value":{"ct":805,"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","pos":{"ct":2,"x":0,"y":0,"an":0,"w":100,"h":100}},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":0,"obj_id":5}]},{"ct":904,"id":13,"topic":"邀请嘉宾","children":[{"ct":904,"id":131,"topic":"嘉宾1","children":[{"ct":904,"id":1311,"topic":"名称1","children":[],"kind":3,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"名称1","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1,"obj_id":13}]},{"ct":904,"id":1312,"topic":"头像1","children":[],"kind":3,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"头像1","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1,"obj_id":14}]}],"kind":2,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"嘉宾1","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1}]},{"ct":904,"id":132,"topic":"嘉宾2","children":[{"ct":904,"id":1321,"topic":"名称2","children":[],"kind":3,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"名称2","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1,"obj_id":15}]},{"ct":904,"id":1322,"topic":"头像2","children":[],"kind":3,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"头像2","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1,"obj_id":16}]}],"kind":2,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"嘉宾2","min":1,"max":3},"child_max":1,"h":0,"o":0,"p":0,"relations":[{"page_index":1}]}],"kind":1,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"邀请嘉宾","min":1,"max":4},"child_max":2,"h":0,"o":0,"p":0,"relations":[{"page_index":1}]}],"kind":0,"type":1,"value":{"ct":808,"type":20,"id":0,"value":"会议邀请函","min":1,"max":5},"child_max":1,"h":0,"o":0,"p":0,"relations":[]}}';
        this.fromJson(JSON.parse(str),3);
        
//        var n: SJGNodeV3=this.first();
//        while(n) { 
//            console.log(n.id+' '+n.topic+' '+n.toJson()+'\n');
//            
//            n=this.next();
//        }
        
//        alert(this.root.toJson());
        return;
//        // kind:自身加子的整体的类型(0根节点/1数组/2结构体/3叶节点)
//        // type:自身（不包含子）的表现形式1(0无/1文字/2图片/3图表/4表格)
//        var root: SJGNodeV3=new SJGNodeV3();
//        root.kind=SJGNodeV3.SJGNODE_KIND_0;
//        root.type=SJGNodeV3.SJGNODE_TYPE_1;
//        root.id=1;
//        root.topic="会议邀请函"
//        this.root=root;
//        
//        // 子节点：会议主题
//        var sub1: SJGNodeV3=new SJGNodeV3();
//        sub1.kind=SJGNodeV3.SJGNODE_KIND_3;
//        sub1.type=SJGNodeV3.SJGNODE_TYPE_1;
//        sub1.id=11;
//        sub1.topic="会议主题"
//        sub1.page_index=0;
//        var t: SJGReplaceItemText=new SJGReplaceItemText();
//        t.value="第五届国际互联网大会"
//        t.min=0;
//        t.max=t.value.length;
//        sub1.value=t;
//        root.children.push(sub1);
//        
//        // 子节点：活动图片
//        var sub1: SJGNodeV3=new SJGNodeV3();
//        sub1.kind=SJGNodeV3.SJGNODE_KIND_3;
//        sub1.type=SJGNodeV3.SJGNODE_TYPE_2;
//        sub1.id=11;
//        sub1.topic="活动图片"
//        sub1.page_index=1;
//        var img: SJGReplaceItemImage=new SJGReplaceItemImage();
//        img.value="http://127.0.0.1:3001/1.jpg";
//        img.pos.w=100;
//        img.pos.h=100;
//        sub1.value=img;
//        root.children.push(sub1);
//        
    }
}//SJG_MARK_END-------------------------